package com.three.work.controller.app;


import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.three.work.controller.BaseController;
import com.three.work.controller.helper.$;
import org.springframework.web.bind.annotation.RequestMapping;
import com.three.work.bean.result.ResultPage;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ResponseBody;

import com.three.work.bean.Sign;
import com.three.work.controller.model.ApiResultModel;

import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 张子艺
 * @since 2020-04-02
 */
@Controller
@RequestMapping("/app/sign")
public class SignController extends BaseController {
     /** 主页面 */
    @RequestMapping("main.html")
    public String main(){
       return  "sign/SignMain";
    }
    /** 添加页面跳转
     * @return 返回页面
     */
    @RequestMapping(value = "/addSignHtml")
    public String addSignHtml( ){
             return "sign/SignAdd";
    }
    /** 编辑页面跳转
     * @param  id  前端传进的参数
     * @return 返回页面
     */
    @RequestMapping(value = "/editSignHtml")
    public String editSignHtml(Integer id ,Model model ){
           model.addAttribute("sign",signService.selectSignByIdSign(id));
             return "sign/SignEdit";
    }

    /** 查看页面跳转
     * @param  id  前端传进的参数
     * @return 返回页面
     */
    @RequestMapping(value = "/showSignHtml")
    public String showSignHtml(Integer id ,Model model ){

           model.addAttribute("sign",signService.selectSignByIdSign(id));
             return "sign/SignShow";
    }


    /** 分页查询Sign
     * @param page  页码
     * @param limit  查几条
     * @param sign  前端传进的参数
     * @return 返回统一分页信息
     */
    @RequestMapping(value = "/findSignByPage")
    @ResponseBody
    public ResultPage findSignByPage(Integer page, Integer limit,Sign sign){
             return signService.findSignByPage(page,limit,sign);
    }

    /** 添加Sign
     * @param sign  前端传进的参数
     * @return 返回统一信息
     */
    @RequestMapping(value = "/addSign")
    @ResponseBody
    public ApiResultModel saveSign(Sign sign  ){
             return signService.addSign(sign);
    }

    /** 编辑Sign
     * @param sign  前端传进的参数
     * @return 返回统一信息
     */
    @RequestMapping(value = "/editSign")
    @ResponseBody
    public ApiResultModel editSign(Sign sign  ){
             return signService.editSign(sign);
    }


    /** 编辑Sign地点
     * @param sign  前端传进的参数
     * @return 返回统一信息
     */
    @RequestMapping(value = "/editSignLocalAjax")
    @ResponseBody
    public ApiResultModel editSignLocalAjax(String idsStr,String localName,Integer isPay){
        String [] ids=StrUtil.split(idsStr,",");

        for (int i = 0; i < ids.length; i++) {
         Sign sign=signService.getById(Integer.parseInt(ids[i]));
            QueryWrapper queryWrapper=new QueryWrapper();
            queryWrapper.eq("y",sign.getY());
            queryWrapper.eq("m",sign.getM());
            queryWrapper.eq("d",sign.getD());
            queryWrapper.eq("user_id",sign.getUserId());
            List<Sign> signs=signService.list(queryWrapper);
            for (int j = 0; j <signs.size()  ; j++) {
                Sign signUp=signs.get(j);
                signUp.setLocationName(localName);
                signUp.setIsPay(isPay);
                signService.updateById(signUp);
            }
        }
             return $.success();
    }


    /** 编辑Sign地点
     * @param sign  前端传进的参数
     * @return 返回统一信息
     */
    @RequestMapping(value = "/editSignLocal")
    public String editSignLocal(String ids,Model model ){
        model.addAttribute("ids",ids);
             return   "sign/SignEditLocal";
    }



    /** 删除一条Sign
     * @param id  前端传进的ID
     * @return 返回统一信息
     */
    @RequestMapping(value = "/delSign")
    @ResponseBody
    public ApiResultModel delSign( Integer id  ){
             return signService.delSign(id);
    }

    /** 统计签到  人员统计
     * @param id  前端传进的ID
     * @return 返回统一信息
     */
    @RequestMapping(value = "/statisticsSign")
    @ResponseBody
    public ApiResultModel delSign( String time,Integer type ){
        if(StrUtil.isBlank(time)){
            return $.error("时间不正确");
        }
        if(type==null ){
            return $.error("类型不正确");
        }
             return signService.statisticsSign(time,type);
    }

    /** 签到管理
     * @param page  页码
     * @param limit  查几条
     * @param sign  前端传进的参数
     * @return 返回统一分页信息
     */
    @RequestMapping(value = "/signManage")
    @ResponseBody
    public ResultPage signManage(Integer page, Integer limit,String time){

        return signService.signManage(page,limit,time);
    }

    /** 某人某月签到统计
     * @param page  页码
     * @param limit  查几条
     * @param sign  前端传进的参数
     * @return 返回统一分页信息
     */
    @RequestMapping(value = "/statisticsSignByUser")
    @ResponseBody
    public ResultPage statisticsSignByUser(Integer page, Integer limit,String time,Integer userId){
        return signService.statisticsSignByUser(page,limit,time,userId);
    }

    /** 某人某天签到详情
     * @param page  页码
     * @param limit  查几条
     * @param sign  前端传进的参数
     * @return 返回统一分页信息
     */
    @RequestMapping(value = "/signInfoByUser")
    @ResponseBody
    public ApiResultModel signInfoByUser( String time,Integer userId){
        if(StrUtil.isBlank(time)){
            return $.error("时间不正确");
        }
        if(userId==null ){
            return $.error("用户不正确");
        }
        return signService.signInfoByUser( time,userId);
    }

    @RequestMapping("/insertSign")
    @ResponseBody
    public ApiResultModel insertSign(String userId, String isPm, String y, String m, String d, String state){


        return signService.FindInsertSign(userId,isPm,y,m,d,state);
    }

}
